package com.holyn.guofang.model.sharepreference;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.mylibrary.utils.AESUtil;


/**
 * Created by Holyn on 2017/6/5.
 */

public class UserInfoSharePreference {

    private static SharedPreferences sharedPreferences = null;

    private static String seed = "holyn";//aes加密密钥

    public static void newInstance(Context context){
        if (sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
        }
    }

    public static void savePassword(Context context, String password){
        try {
            editorCommit(context, "password", AESUtil.encrypt(seed, password));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getPassword(Context context){
        String password = getString(context, "password", "");
        if (!TextUtils.isEmpty(password)){
            try {
                password = AESUtil.decrypt(seed, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return password;
    }

    public static void saveStatus(Context context, String status){
        editorCommit(context, "status", status);
    }

    public static String getStatus(Context context){
        return getString(context, "status", "");
    }

    public static void saveUserID(Context context, String userid){
        editorCommit(context, "userid", userid);
    }

    public static String getUserID(Context context){
        return getString(context, "userid", "");
    }

    public static void saveLoginID(Context context, String loginid){
        editorCommit(context, "loginid", loginid);
    }

    public static String getLoginID(Context context){
        return getString(context, "loginid", "");
    }

    public static void saveCheckCode(Context context, String checkcode){
        editorCommit(context, "checkcode", checkcode);
    }

    public static String getCheckCode(Context context){
        return getString(context, "checkcode", "");
    }

    public static void saveErr(Context context, String err){
        editorCommit(context, "err", err);
    }

    public static String getErr(Context context){
        return getString(context, "err", "");
    }

    public static void saveUserName(Context context, String username){
        editorCommit(context, "username", username);
    }

    public static String getUserName(Context context){
        return getString(context, "username", "");
    }

    public static void saveName(Context context, String name){
        editorCommit(context, "name", name);
    }

    public static String getName(Context context){
        return getString(context, "name", "");
    }

    public static void saveMobile(Context context, String mobile){
        editorCommit(context, "mobile", mobile);
    }

    public static String getMobile(Context context){
        return getString(context, "mobile", "");
    }

    public static void saveEmail(Context context, String email){
        editorCommit(context, "email", email);
    }

    public static String getEmail(Context context){
        return getString(context, "email", "");
    }

    public static void savecheckMethod(Context context, String checkMethod){
        editorCommit(context, "checkMethod", checkMethod);
    }

    public static String getCheckMethod(Context context){
        return getString(context, "checkMethod", "");
    }

    public static void saveAddress(Context context, String address){
        editorCommit(context, "address", address);
    }

    public static String getAddress(Context context){
        return getString(context, "address", "");
    }

    public static void saveCountry(Context context, String country){
        editorCommit(context, "country", country);
    }

    public static String getCountry(Context context){
        return getString(context, "country", "");
    }

    public static void saveArea(Context context, String area){
        editorCommit(context, "area", area);
    }

    public static String getArea(Context context){
        return getString(context, "area", "");
    }

    public static void saveContact(Context context, String contact){
        editorCommit(context, "contact", contact);
    }

    public static String getContact(Context context){
        return getString(context, "contact", "");
    }

    public static void saveContactTel(Context context, String contactTel){
        editorCommit(context, "contacttel", contactTel);
    }

    public static String getContactTel(Context context){
        return getString(context, "contacttel", "");
    }

    public static void saveContactMobile(Context context, String contactMobile){
        editorCommit(context, "contactmobile", contactMobile);
    }

    public static String getContactMobile(Context context){
        return getString(context, "contactmobile", "");
    }

    public static void saveContactEmail(Context context, String contactEmail){
        editorCommit(context, "contactemail", contactEmail);
    }

    public static String getContactEmail(Context context){
        return getString(context, "contactemail", "");
    }

    public static void saveIsMainDevice(Context context, boolean isMainDevice){
        editorCommit(context, "isMainDevice", isMainDevice);
    }

    public static boolean getIsMainDevice(Context context){
        return getBoolean(context, "isMainDevice", false);
    }

    public static void saveDeviceRemark(Context context, String deviceRemark){
        editorCommit(context, "deviceRemark", deviceRemark);
    }

    public static String getDeviceRemark(Context context){
        return getString(context, "deviceRemark", "");
    }

    public static void saveDeviceCount(Context context, String deviceCount){
        editorCommit(context, "deviceCount", deviceCount);
    }

    public static String getDeviceCount(Context context){
        return getString(context, "deviceCount", "");
    }

    public static void saveCardType(Context context, String cardType){
        editorCommit(context, "cardType", cardType);
    }

    public static String getCardType(Context context){
        return getString(context, "cardType", "");
    }

    public static void saveActiveTime(Context context, String activeTime){
        editorCommit(context, "activeTime", activeTime);
    }

    public static String getActiveTime(Context context){
        return getString(context, "activeTime", "");
    }

    public static void saveEndTime(Context context, String endTime){
        editorCommit(context, "endTime", endTime);
    }

    public static String getEndTime(Context context){
        return getString(context, "endTime", "");
    }

    public static void saveValidMethod(Context context, String validMethod){
        editorCommit(context, "validMethod", validMethod);
    }

    public static String getValidMethod(Context context){
        return getString(context, "validMethod", "");
    }

    public static void saveRemainCount(Context context, String remainCount){
        editorCommit(context, "remainCount", remainCount);
    }

    public static String getRemainCount(Context context){
        return getString(context, "remainCount", "");
    }

    public static void saveEquipmentCount(Context context, String equipmentCount){
        editorCommit(context, "equipmentCount", equipmentCount);
    }

    public static String getEquipmentCount(Context context){
        return getString(context, "equipmentCount", "");
    }

    public static void saveAttachedCardCount(Context context, String attachedCardCount){
        editorCommit(context, "attachedCardCount", attachedCardCount);
    }

    public static String getAttachedCardCount(Context context){
        return getString(context, "attachedCardCount", "");
    }



    public static void saveUserInfo(Context context, UserInfoWo userInfoWo){
        saveStatus(context, String.valueOf(userInfoWo.getStatus()));
        saveUserID(context, String.valueOf(userInfoWo.getUserID()));
        saveLoginID(context, String.valueOf(userInfoWo.getLoginID()));
        saveCheckCode(context, userInfoWo.getCheckCode());
        saveErr(context, userInfoWo.getErr());

        UserInfoWo.UserInfoBean userInfoBean = userInfoWo.getUserInfo();
        if (userInfoBean != null){
            saveUserName(context, userInfoBean.getUserName());
            saveName(context, userInfoBean.getName());
            saveMobile(context, userInfoBean.getMobile());
            saveEmail(context, userInfoBean.getEmail());
            savecheckMethod(context, userInfoBean.getCheckMethod());
            saveAddress(context, userInfoBean.getAddress());
            saveCountry(context, userInfoBean.getCountry());
            saveArea(context, userInfoBean.getArea());
            saveContact(context, userInfoBean.getContact());
            saveContactTel(context, userInfoBean.getContactTel());
            saveContactMobile(context, userInfoBean.getContactMobile());
            saveContactEmail(context, userInfoBean.getContactEmail());
            saveDeviceRemark(context, userInfoBean.getDeviceRemark());
            saveIsMainDevice(context, userInfoBean.getIsMainDevice());
            saveDeviceCount(context, String.valueOf(userInfoBean.getDeviceCount()));

            UserInfoWo.UserInfoBean.UserCardBean userCardBean = userInfoBean.getUserCard();
            if (userCardBean != null){
                saveCardType(context, String.valueOf(userCardBean.getCardType()));
                saveActiveTime(context, userCardBean.getActiveTime());
                saveEndTime(context, userCardBean.getEndTime());
                saveValidMethod(context, String.valueOf(userCardBean.getValidMethod()));
                saveRemainCount(context, String.valueOf(userCardBean.getRemainCount()));
                saveEquipmentCount(context, String.valueOf(userCardBean.getEquipmentCount()));
                saveAttachedCardCount(context, String.valueOf(userCardBean.getAttachedCardCount()));
            }
        }

    }

    public static UserInfoWo getUserInfo(Context context){
        UserInfoWo userInfoWo = new UserInfoWo();
        userInfoWo.setStatus(Integer.valueOf(getStatus(context)));
        userInfoWo.setUserID(Integer.valueOf(getUserID(context)));
        userInfoWo.setLoginID(Integer.valueOf(getLoginID(context)));
        userInfoWo.setCheckCode(getCheckCode(context));
        userInfoWo.setErr(getErr(context));

        UserInfoWo.UserInfoBean userInfoBean = new UserInfoWo.UserInfoBean();
        userInfoBean.setUserName(getUserName(context));
        userInfoBean.setName(getName(context));
        userInfoBean.setMobile(getMobile(context));
        userInfoBean.setEmail(getEmail(context));
        userInfoBean.setCheckMethod(getCheckMethod(context));
        userInfoBean.setAddress(getAddress(context));
        userInfoBean.setCountry(getCountry(context));
        userInfoBean.setArea(getArea(context));
        userInfoBean.setContact(getContact(context));
        userInfoBean.setContactTel(getContactTel(context));
        userInfoBean.setContactMobile(getContactMobile(context));
        userInfoBean.setContactEmail(getContactEmail(context));
        userInfoBean.setIsMainDevice(getIsMainDevice(context));
        userInfoBean.setDeviceRemark(getDeviceRemark(context));
        userInfoBean.setDeviceCount(Integer.valueOf(getDeviceCount(context)));

        UserInfoWo.UserInfoBean.UserCardBean userCardBean = new UserInfoWo.UserInfoBean.UserCardBean();
        userCardBean.setCardType(Integer.parseInt(getCardType(context)));
        userCardBean.setActiveTime(getActiveTime(context));
        userCardBean.setEndTime(getEndTime(context));
        userCardBean.setValidMethod(Integer.parseInt(getValidMethod(context)));
        userCardBean.setRemainCount(Integer.parseInt(getRemainCount(context)));
        userCardBean.setEquipmentCount(Integer.parseInt(getEquipmentCount(context)));
        userCardBean.setAttachedCardCount(Integer.parseInt(getAttachedCardCount(context)));

        userInfoBean.setUserCard(userCardBean);
        userInfoWo.setUserInfo(userInfoBean);

        return  userInfoWo;
    }

    private static void editorCommit(Context context, String key, String value){
        newInstance(context);
        if (value == null) value = "";
        sharedPreferences.edit().putString(key, value).commit();
    }

    private static String getString(Context context, String key, String defValue){
        newInstance(context);
        return sharedPreferences.getString(key, defValue);
    }

    private static void editorCommit(Context context, String key, boolean value){
        newInstance(context);
        sharedPreferences.edit().putBoolean(key, value).commit();
    }

    private static boolean getBoolean(Context context, String key, boolean defValue){
        newInstance(context);
        return sharedPreferences.getBoolean(key, defValue);
    }

}
